package com.palm.hubo2o.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.palm.hubo2o.R;
import com.palm.hubo2o.model.Order;
import com.palm.palmutils.TimeUtils;
import com.palm.palmutils.widget.CellView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by wongqi on 16/6/21.
 */
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderHolder> {
    private List<Order> items;
    private SparseBooleanArray itemsPendingRemoval;
    private LayoutInflater inflater;

    public OrderAdapter() {
        itemsPendingRemoval = new SparseBooleanArray();
    }

    public void setOrders(List<Order> orders, boolean refresh) {
        if (items == null)
            items = new ArrayList<>();
        if (refresh) {
            items.clear();
            items.addAll(orders);
            notifyDataSetChanged();
        } else {
            items.addAll(orders);
            notifyItemRangeInserted(items.size() - orders.size(), orders.size());
        }
    }

    public void pendingRemoval(int position) {
        itemsPendingRemoval.put(position, true);
    }

    public boolean isPendingRemoval(int position) {
        return itemsPendingRemoval.get(position);
    }

    @Override
    public OrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        if (inflater == null)
//            inflater = LayoutInflater.from(parent.getContext());
//        View v = inflater.inflate(R.layout.item_order, parent, false);
//        final OrderHolder holder = new OrderHolder(v);
////        holder.delete.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View v) {
////                int position = holder.getAdapterPosition();
////                items.remove(position);
////                itemsPendingRemoval.delete(position);
////                notifyItemRemoved(position);
////            }
////        });
////        holder.undo.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View v) {
////                int position = holder.getAdapterPosition();
////                itemsPendingRemoval.put(position, false);
////                notifyItemChanged(position);
////            }
////        });
//        return holder;
        Context context = parent.getContext();
        CellView cellView = new CellView(context);
        cellView.setDescSubColor(ContextCompat.getColor(context, R.color.red));
        return new OrderHolder(cellView);
    }

    @Override
    public void onBindViewHolder(OrderHolder holder, int position) {
//        if (!itemsPendingRemoval.get(position)) {//未弹出删除按钮
//            final Order order = items.get(position);
//            holder.cell.setIcon(GameUtils.getGameIconRes(order.getGameId()));
//            holder.cell.setIconDesc(order.getGameId().getName());
//            holder.cell.setTitle(order.getIssueNo() + "");
//            holder.cell.setTitleSub(order.getOrderMoney());
//            holder.cell.setTitleFooter(TimeUtils.formatTime("yyyy-MM-dd", order.getCreateTime()));
//            holder.cell.setDesc(order.getOrderTypeShow());
//            holder.cell.setDescSub(order.getPrizeMoney());
//            holder.cell.setDescTint(order.getOrderStatusShow());
//            holder.cell.setVisibility(View.VISIBLE);
//            holder.delete.setVisibility(View.GONE);
//            holder.undo.setVisibility(View.GONE);
//        } else {
//            holder.cell.setVisibility(View.GONE);
//            holder.delete.setVisibility(View.VISIBLE);
//            holder.undo.setVisibility(View.VISIBLE);
//        }
        final Order order = items.get(position);
        holder.cell.setIcon(order.getGameId().getIconId());
        holder.cell.setIconDesc(order.getGameId().getName());
        holder.cell.setTitle(order.getIssueNo() + "");
        holder.cell.setTitleSub(String.format(Locale.getDefault(), "%d元", order.getOrderMoney()));
        holder.cell.setTitleFooter(TimeUtils.formatTime("yyyy-MM-dd", order.getCreateTime()));
        holder.cell.setDesc(order.getOrderTypeShow());
        if (order.getPrizeMoney() <= 0) {//未中奖
            holder.cell.setDescTint(order.getOrderStatusShow());
            holder.cell.setDescSub(null);
        } else {
            holder.cell.setDescTint(null);
            holder.cell.setDescSub(order.getOrderStatusShow());
        }
    }

    @Override
    public int getItemCount() {
        return items == null ? 0 : items.size();
    }

    public class OrderHolder extends RecyclerView.ViewHolder {
        //        @Bind(R.id.cell_order)
//        public CellView cell;
//        @Bind(R.id.delete_order)
//        public TextView delete;
//        @Bind(R.id.undo_order)
//        public TextView undo;
        public CellView cell;

        public OrderHolder(View itemView) {
            super(itemView);
            cell = (CellView) itemView;
        }

    }
}
